/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.processor;

import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.log4j.Logger;

import javax.xml.namespace.QName;

public class ChangeTopologyProcessor implements Processor{

    private static final Logger logger = Logger.getLogger(ChangeTopologyProcessor.class);

    public SOAPEnvelope processMessage(SOAPEnvelope requestEnvelope) {

        try {
            SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
            SOAPEnvelope responseEnvelop = factory.getDefaultEnvelope();

            OMElement topologyElement = requestEnvelope.getBody()
                    .getFirstChildWithName(new QName("TopologyDetails"))
                    .getFirstChildWithName(new QName("Topology"));

            OMElement responseElement = factory.createOMElement("Response", null);
            responseElement.setText("Topology changed to " +topologyElement.getText() + " successfully");
            responseEnvelop.getBody().addChild(responseElement);

            return responseEnvelop;
        }
        catch (NullPointerException ex)
        {
            logger.error("Error in processing received change topology message", ex);
            return null;
        }
        catch (OMException ex)
        {
            logger.error("Error in processing received change topology message", ex);
            return null;
        }
    }
}
